package com.sandu.lib_common.base

import android.app.Activity
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.multidex.MultiDex
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.BuildConfig
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.sandu.lib_common.event.AppViewModel
import com.sandu.lib_common.event.EventViewModel
import com.sandu.lib_common.ui.loadCallBack.EmptyCallback
import com.sandu.lib_common.ui.loadCallBack.ErrorCallback
import com.sandu.lib_ui.callback.LoadingCallback
import com.sandu.lib_mvvm.base.BaseApp
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV
/**
 * 作者　: sandu
 * 时间　: 2024/11/20
 * 描述　: BaseApplication 错误页面 各个模块需要从application获取的变量等
 */
//Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
val appViewModel: AppViewModel by lazy { BaseApplication.appViewModelInstance }

//Application全局的ViewModel，用于发送全局通知操作
val eventViewModel: EventViewModel by lazy { BaseApplication.eventViewModelInstance }

open class BaseApplication : BaseApp() {
    lateinit var activityLogin: Class<out Activity>
    private lateinit var activityStart: Class<out Activity>
    private lateinit var activityError: Class<out Activity>

    companion object {

        lateinit var instance: BaseApplication
        lateinit var eventViewModelInstance: EventViewModel
        lateinit var appViewModelInstance: AppViewModel


    }


    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate() {
        super.onCreate()
//        val catchHandler: CrashHandle = CrashHandle.getInstance()
//        catchHandler.init(applicationContext)
        if (BuildConfig.DEBUG) {           // These two lines must be written before init, otherwise these configurations will be invalid in the init process
            ARouter.openLog();     // Print log
            ARouter.openDebug();   // Turn on debugging mode (If you are running in InstantRun mode, you must turn on debug mode! Online version needs to be closed, otherwise there is a security risk)
        }
        ARouter.init(this); // As early as possible, it is recommended to initialize in the Application


        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")
        instance = this
        eventViewModelInstance = getAppViewModelProvider().get(EventViewModel::class.java)
        appViewModelInstance = getAppViewModelProvider().get(AppViewModel::class.java)
        MultiDex.install(this)
        //界面加载管理 初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback())//加载
            .addCallback(ErrorCallback())//错误
            .addCallback(EmptyCallback())//空
            .setDefaultCallback(SuccessCallback::class.java)//设置默认加载状态页
            .commit()
        //初始化Bugly
        val context = applicationContext
        // 获取当前包名
        val packageName = context.packageName
        // 获取当前进程名
        val processName = com.sandu.lib_common.ext.getProcessName(android.os.Process.myPid())
        // 设置是否为上报进程
        val strategy = CrashReport.UserStrategy(context)
        strategy.isUploadProcess = processName == null || processName == packageName



    }
    fun setActivity(
        java: Class<out Activity>,
        java1: Class<out Activity>,
        java2: Class<out Activity>
    ) {
        this.activityStart = java
        this.activityError = java1
        this.activityLogin = java2

        //防止项目崩溃，崩溃后打开错误界面
        CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
            .enabled(true)//是否启用CustomActivityOnCrash崩溃拦截机制 必须启用！不然集成这个库干啥？？？
            .showErrorDetails(false) //是否必须显示包含错误详细信息的按钮 default: true
            .showRestartButton(false) //是否必须显示“重新启动应用程序”按钮或“关闭应用程序”按钮default: true
            .logErrorOnRestart(false) //是否必须重新堆栈堆栈跟踪 default: true
            .trackActivities(true) //是否必须跟踪用户访问的活动及其生命周期调用 default: false
            .minTimeBetweenCrashesMs(2000) //应用程序崩溃之间必须经过的时间 default: 3000
            .restartActivity(activityStart) // 重启的activity
            .errorActivity(activityError) //发生错误跳转的activity
            .apply()


    }

//    private fun getJavaClass(activity: Activity): Class<out Activity>? {
//        return activity::class.java
//    }
}